package cn.ylz.ylmall.content.controller;

import cn.ylz.ylmall.account.security.CurrentPrincipal;
import cn.ylz.ylmall.common.web.JsonResult;
import cn.ylz.ylmall.content.pojo.param.CollectAddNewParam;
import cn.ylz.ylmall.content.pojo.param.Count;
import cn.ylz.ylmall.content.pojo.vo.CountVO;
import cn.ylz.ylmall.content.service.ICollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/v1/collect")
@Validated
@Api(tags = "2.4. 收藏模块")
public class CollectController {
    @Autowired
    private ICollectService collectService;

    @PostMapping("/addNew")
    @ApiOperation("添加收藏")
    public JsonResult addNew(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                             @Valid CollectAddNewParam collectAddNewParam) {
        log.debug("开始处理【添加收藏】的请求，参数：{}", collectAddNewParam);
       JsonResult isCollect = collectService.addNew(currentPrincipal, collectAddNewParam);
        return JsonResult.ok(isCollect);
    }
    @GetMapping("")
    @ApiOperation("根据内容ID查询收藏数量")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "contentId", value = "内容ID", required = true, dataType = "Long")
    })
    public JsonResult count(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                            @Valid @RequestParam Long contentId) {
        log.debug("开始处理【根据内容ID统计收藏数量】的请求，参数：{}", contentId);
        CountVO count = collectService.count(contentId);
        return JsonResult.ok(count);
    }
    @PostMapping("/IsCollect")
    @ApiOperation("查询是否已经收藏")
    public Boolean IsLike(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                          @Valid @RequestParam Long contentId){
        log.debug("开始处理查询是否已经点赞 参数{}",contentId);
        return collectService.selectIsCollect(currentPrincipal.getId(),contentId);
    }


}
